package decorator_pattern;

/**
 * ConcreteDecorator 通过 继承 Decorator 得到 component  然后具体实现 component的行为
 * 构造函数是接受component参数关键
 * @author lynn
 * @date 18-12-8 上午7:15
 */
public class ConcreteDecoratorB extends Decorator {

    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    /**
     * get all columns
     *
     * @return
     */
    @Override
    public int getColumns() {
        return 1 + this.component.getColumns() + 1;
    }

    /**
     * get all rows
     *
     * @return
     */
    @Override
    public int getRows() {
        return 1 + this.component.getRows() + 1;
    }

    /**
     * get text for given row number
     *
     * @param row
     * @return
     */
    @Override
    public String getRowText(int row) {

        if( 0 == row){
            return "+" + makeLine('-',this.component.getColumns()) + "+";
        }else if( row == this.component.getRows() + 1 ){
            return "+" + makeLine('-',this.component.getColumns()) + "+";
        }else{
            return "|" + this.component.getRowText(row - 1) + "|";
        }

    }


    private String makeLine(char ch,int count){
        StringBuilder res = new StringBuilder();

        for (int i = 0; i < count; i++) {
            res.append(ch);
        }

        return res.toString();
    }

}
